using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Outlook = Microsoft.Office.Interop.Outlook;


namespace ToodleOutlook
{
    public class ToodleDoAPIWrapper
    {
        private string _apiKey;

        public ToodleDoAPIWrapper()
        {
            _apiKey = generateKey();
        }

        public static String generateKey()
        {
            String userID = "td475d918273742";
            String tokenUrl = "http://www.toodledo.com/api.php?method=getToken;userid=" + userID;
            try
            {
                XmlReader theReader = new XmlTextReader(tokenUrl);
                theReader.MoveToContent();
                String theToken = theReader.ReadElementContentAsString();
                String theKey = Util.MD5(Util.MD5("fraggle") + theToken + userID);
                return theKey;

            }
            catch (Exception ex)
            {
                return ex.StackTrace;
            }
        }

        public GenericTaskItem[] getTasks()
        {
            string taskUrl = "http://www.toodledo.com/api.php?method=getTasks;key=" + _apiKey;
            //try
            //{
                GenericTaskItem[] theNewItems = null;
                int taskIndex = 0;
                string theImportance;
                string theDueDate;
                XmlReader theReader = new XmlTextReader(taskUrl);
                XmlDocument theTasksDoc = new XmlDocument();
                theTasksDoc.Load(theReader);
                XmlNodeList theTasks = theTasksDoc.SelectNodes("task[/completed=string(.)]");
                theNewItems = new GenericTaskItem[theTasks.Count];
                foreach (XmlNode theTaskNode in theTasks)
                {
                    theNewItems[taskIndex] = new GenericTaskItem();
                    theNewItems[taskIndex].Subject = theTaskNode.SelectSingleNode("title").InnerText;
                    theDueDate = theTaskNode.SelectSingleNode("duedate").InnerText;
                    if(theDueDate!=String.Empty) theNewItems[taskIndex].DueDate = Convert.ToDateTime(theDueDate);
                    theNewItems[taskIndex].Categories = theTaskNode.SelectSingleNode("folder").InnerText;
                    theImportance = theTaskNode.SelectSingleNode("priority").InnerText;
                    theNewItems[taskIndex].Importance = (theImportance.Equals("1") ? Outlook.OlImportance.olImportanceNormal : Outlook.OlImportance.olImportanceHigh);
                    theNewItems[taskIndex].Body = theTaskNode.SelectSingleNode("note").InnerText;
                    taskIndex++;
                }
                return theNewItems;

            //}
            
            //catch (Exception ex)
            //{
            //    throw ex;
            //}

        }


     public string addTask(Outlook.TaskItem theTask)
     {
         Boolean blRet = false;
         string taskUrl = "http://www.toodledo.com/api.php?method=addTask;key=" + _apiKey + ";";
         taskUrl += "title=" + theTask.Subject + ";";
         taskUrl += "priority=" + theTask.Importance + ";";
         taskUrl += "duedate=" + theTask.DueDate.ToString("yyyy-MM-dd") +";";
         taskUrl += "note=" + theTask.Body.Replace("&","%26").Replace(";","%3B");
         XmlReader theReader = new XmlTextReader(taskUrl);
         theReader.MoveToContent();
         string strRet = theReader.ReadElementContentAsString();

         return strRet;
     }
    }
}
